//
//  ZYCNewsBulletinDetailWebCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/8.
//

#import "ZYCNewsBulletinDetailWebCell.h"
@interface ZYCNewsBulletinDetailWebCell ()<UIScrollViewDelegate,WKNavigationDelegate,WKUIDelegate>

@end
@implementation ZYCNewsBulletinDetailWebCell

- (void)setContent:(NSString *)content{
    _content = content;
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.webView];
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(10.0f);
            make.bottom.right.equalTo(self.contentView).offset(-10.0f);
        }];
    }
    return self;
}

-(WKWebView *) webView{
    if (!_webView) {
        NSString *jScript =
        @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport');\
        meta.setAttribute('content', 'width=device-width');\
        document.getElementsByTagName('head')[0].appendChild(meta); \
        var imgs = document.getElementsByTagName('img');\
        for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';};";
       WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
       WKUserContentController *wkUController = [[WKUserContentController alloc] init];
       [wkUController addUserScript:wkUScript];
       WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
        webConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webConfig];
        _webView.navigationDelegate = self;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
//        _webView.UIDelegate = self;
        _webView.scrollView.scrollEnabled = NO;

        
    }
    return _webView;
}
#pragma mark - WKNavigationDelegate

/* 页面加载完成 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content=\"width=device-width, user-scalable=no\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
     
}

#pragma mark -WKNavigationDelegate

//WKWebview的使用后加载h5页面后不能正常点击里面的链接
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    //如果是跳转一个新页面
    if (navigationAction.targetFrame == nil) {
        [webView loadRequest:navigationAction.request];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

//-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
//    if (!navigationAction.targetFrame.isMainFrame) {
//        [webView loadRequest:navigationAction.request];
//    }
//    return nil;
//
//}

@end
